﻿#include "contactuser.h"
#include <QApplication>
#include <QPainter>
#include "characterset.h"

static const QColor bgcolor = "#4da9ea";

ContactUser::ContactUser() : p_org_id(0)
{

}

bool ContactUser::operator ==(const ContactUser &other) const
{
    return this->uid == other.uid && this->account == other.account && this->phone == other.phone && this->name == other.name;
}

QVariant ContactUser::decoration(qint64 uid, QString name, int width, int height, QFont font)
{
    QPixmap pixmap(width, height);
    pixmap.fill(Qt::transparent);
    {
        QPainter p(&pixmap);
        p.setRenderHint(QPainter::Antialiasing);
        p.setRenderHint(QPainter::TextAntialiasing);
        p.setPen(Qt::NoPen);
        p.setBrush(bgcolor);
        p.drawEllipse(0, 0, width, height);

        //font.setBold(true);
        font.setPixelSize(height * 2 / 5);
        p.setFont(font);
        p.setPen(Qt::white);
        if(name.isEmpty())
            name = QString::number(uid);
        p.drawText(pixmap.rect(), Qt::AlignCenter, name.right(2));
    }
    return pixmap;
}

QVariant ContactUser::decoration(int width, int height, const QFont &font) const
{
    return decoration(uid, display(), width, height, font);
}

QString ContactUser::display() const
{
    if(name.isEmpty())
        return QString::number(uid);
    return name;
}

QString ContactUser::getName(bool notEmpty) const
{
    if(notEmpty && name.isEmpty())
        return QString::number(uid);
    return name;
}

QString ContactUser::getToolTip() const
{
    QStringList tooltip;
    if(!account.isEmpty())
        tooltip << QObject::trECSUTF8("账号: ") + account;
    if(!name.isEmpty())
        tooltip << QObject::trECSUTF8("名称: ") + name;
    if(!phone.isEmpty())
        tooltip << QObject::trECSUTF8("电话: ") + phone;
    return tooltip.join('\n');
}
